HANDBOOKS=en
IN_BASENAME=spiff_workflow
OUT_BASENAME=spiff_workflow
PUBLISH_PATH=/home/sab/backups/code/www/docs.debain.org/$(OUT_BASENAME)
PUBLISH_HOST=root@debain.org

all: handbook apidocs

figures:
	# Generate GraphViz figures. Unfortunately, pdflatex does not work when 
	# including these figures in PDF, tex, or PS format, so we are stuck with 
	# png.
	for FILE in figures/*.dot; do \
		DESTFILE=`echo $$FILE | sed 's/.dot\$$//'`.png; \
		dot -Tpng -Gmargin=0 $$FILE -o $$DESTFILE; \
	done

#pdf: figures
pdf:
	# Generate a latex file that defines the version number.
	VERSION=`python ../setup.py --version`; \
	echo '% This file is automatically generated; do not edit' > version.tex; \
	echo "\\\newcommand{\\\productversion}{$$VERSION }" >> version.tex
	
	# Run each call of pdflatex twice, required to resolve references.
	for LOCALE in $(HANDBOOKS); do \
		FILE=$(IN_BASENAME).$$LOCALE; \
		pdflatex -halt-on-error $$FILE.tex && pdflatex $$FILE.tex; \
		rm $$FILE.aux $$FILE.log $$FILE.out $$FILE.toc; \
		[ ! -e handbook/$$LOCALE ] && mkdir -p handbook/$$LOCALE/; \
		mv $$FILE.pdf handbook/$$LOCALE/$(OUT_BASENAME).$$LOCALE.pdf; \
	done

handbook: pdf
	for LOCALE in $(HANDBOOKS); do \
		FILE=$(IN_BASENAME).$$LOCALE; \
		latex2html -nonavigation -toc_depth 5 -split 0 -html_version 4.0,unicode -mkdir -dir handbook/$$LOCALE/ $$FILE.tex; \
	done

apidocs:
	python mkapidoc.py

clean:
	rm -Rf api handbook
	rm -f *.aux *.log *.out *.pdf *.toc figures/*.png

publish: handbook apidocs
	ssh root@debain.org "mkdir -p $(PUBLISH_PATH)"
	rsync -azr handbook/* api/* $(PUBLISH_HOST):$(PUBLISH_PATH)/
	ssh $(PUBLISH_HOST) "chown -R www-data:www-data $(PUBLISH_PATH)/"
	make clean
